package com.sskj.gocoin.p.activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.sskj.gocoin.MyAppLication;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.m.HttpConfig;
import com.sskj.gocoin.m.bean.HttpData;
import com.sskj.gocoin.m.bean.UserBean;
import com.sskj.gocoin.m.http.JsonCallBack;
import com.sskj.gocoin.p.base.BasePresenter;
import com.sskj.gocoin.utils.SPUtils;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.activity.LoginActivity;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/03/30
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class LoginPresenter extends BasePresenter<LoginActivity> {
    /**
     * 登录
     *
     * @param mobile 账号
     * @param pws    密码
     */
    public void login(String mobile, String pws) {
        OkGo.<HttpData<UserBean>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                .params("mobile", mobile)
                .params("opwd", pws)
                .execute(new JsonCallBack<HttpData<UserBean>>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserBean>> response) {
                        HttpData<UserBean> httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_MOBILE, mobile);
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_TOKEN, httpListData.getData().getToken());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_ACCOUNT, httpListData.getData().getAccount());
                            SPUtils.put(MyAppLication.getApplication(), Constants.GOOGLETYPE, httpListData.getData().getIs_start_google());
                            //重新初始化公共头
                            mView.loginSuccess(httpListData.getData().getIs_start_google());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 获取邮箱验证码
     */
    public void sendEmail(String email) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SEND_EMAIL)
                .params("email", email)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.startCount();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }
                });
    }

    /**
     * 获取短信验证码
     */
    public void sendCode(String account) {
        OkGo.<HttpData>post(HttpConfig.BASE_URL + HttpConfig.SMS_SEND)
                .params("mobile", account)
                .params("type", 3)
                .execute(new JsonCallBack<HttpData>(LoginPresenter.this) {
                    @Override
                    public void _onSuccess(Response<HttpData> response) {
                        HttpData httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            mView.startCount();
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }

                });
    }

    /**
     * 验证码登录
     */
    public void yzmLogin(String account, String code) {
        OkGo.<HttpData<UserBean>>post(HttpConfig.BASE_URL + HttpConfig.YZM_LOGIN)
                .params("mobile", account)
                .params("code", code)
                .execute(new JsonCallBack<HttpData<UserBean>>(LoginPresenter.this) {
                    @Override
                    public void _onSuccess(Response<HttpData<UserBean>> response) {
                        HttpData<UserBean> httpListData = response.body();
                        if (httpListData.getStatus() == 200) {
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_MOBILE, account);
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_TOKEN, httpListData.getData().getToken());
                            SPUtils.put(MyAppLication.getApplication(), Constants.SP_ACCOUNT, httpListData.getData().getAccount());
                            SPUtils.put(MyAppLication.getApplication(), Constants.GOOGLETYPE, httpListData.getData().getIs_start_google());
                            //重新初始化公共头
                            mView.loginSuccess(httpListData.getData().getIs_start_google());
                        } else {
                            ToastUtil.showShort(httpListData.getMsg());
                        }
                    }

                });
    }
}
